import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import departments from './modules/departments'
import employees from './modules/employees'
import settings from './modules/settings'
import permissions from './modules/permissions'
import approvals from './modules/approvals'
import attendances from './modules/attendances'
import salarys from './modules/salarys'
import social_securitys from './modules/social_securitys'
// 创建动态路由表
export const asyncRoutes = [
  // 放动态路由对象  的模块化
  departments,
  settings,
  employees,
  permissions,

  approvals,
  attendances,
  salarys,
  social_securitys
]
//路由懒加载中的魔法注释
/* webpackChunkName:"login"*/
// 静态路由表
export const constantRoutes = [
  {
    path: '/import',
    component: () => import(/* webpackChunkName:"import"*/'@/views/import/index'),
    hidden: true
  },
  {
    path: '/demo',
    component: () => import(/* webpackChunkName:"demo"*/'@/views/demo/index'),
    hidden: true
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName:"login"*/'@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import(/* webpackChunkName:"404"*/'@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import(/* webpackChunkName:"dashboard"*/'@/views/dashboard/index'),
      meta: { title: '主页~Dashboard', icon: 'dashboard' }
    }]
  },
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }    //这里因为后面添加动态路由时 刷新会出现404  (刷新会先执行*  404)
]
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes]  //最终生成路由(由静态路由和动态路由合并)
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465

// 清空路由   这一般在路由守卫里面用
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
